package com.bingkaimedia.merchantbeta.libs;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class ConnectionChangeReceiver extends BroadcastReceiver
{ 
	    private static final String TAG = "NetworkStateReceiver";

	    @Override
	    public void onReceive(final Context context, final Intent intent) {

	        Log.d(TAG, "Network connectivity change");
	        AlertDialogManager alert = new AlertDialogManager();
	        
	        if (intent.getExtras() != null) {
	            final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	            final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

	            if (ni != null && ni.isConnectedOrConnecting()) {
	                Log.i(TAG, "Network " + ni.getTypeName() + " connected"); 
	                Toast.makeText(context, "Terhubung dengan internet, via " + ni.getTypeName() + "", Toast.LENGTH_LONG).show();
	                
	                
	            } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
	                Log.d(TAG, "There's no network connectivity");
	                Toast.makeText(context, "Tidak terhubung dengan internet.", Toast.LENGTH_LONG).show();
	                alert.showAlertDialog(context, "Information", "Tidak terhubung dengan internet.", false);
	            }
	        }
	    }
	 
 
}